package com.boot2523.common;


import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 声明交换机、队列以及它们之间的绑定关系
 */
@Configuration
public class RabbitMQConfig {
    // 定义队列名称
    public static final String ORDER_QUEUE = "orderQueue";
    public static final String EXCHANGE_NAME = "orderExchange";
    // 声明队列
    @Bean
    public Queue orderQueue() {
        return new Queue(ORDER_QUEUE, true); // true 表示持久化队列
    }
    // 声明交换机

    /**
     * opicExchange支持基于通配符的路由机制
     * @return
     */
    @Bean
    public TopicExchange orderExchange() {
        return new TopicExchange(EXCHANGE_NAME);
    }

    /**
     * 定义交换机和队列之间的绑定关系
     * @param orderQueue
     * @param orderExchange
     * @return
     */
    // 绑定队列到交换机
    @Bean
    public Binding binding(Queue orderQueue, TopicExchange orderExchange) {
        return BindingBuilder.bind(orderQueue).to(orderExchange).with("order.#");
    }
}
